package com.example.demo.controllers;

import com.example.demo.common.utils.ToolsUtil;
import com.example.demo.entity.YG;
import com.example.demo.services.YGService;
import com.example.demo.services.YGService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

/**
 * @Author: Ding XiJie
 */
@RequestMapping("/yg")
@Controller
public class YGController {
    @Autowired
    private YGService ygService;

    /**
     * @Author: Ding XiJie
     * @Description: 添加员工
     */
    @RequestMapping("/save")
    @ResponseBody
    public Map<String, Object> save(YG yg){
        try {
            yg = ygService.save(yg);
            return ToolsUtil.returnMessage(0,"添加成功");
        }catch (Exception e){
            e.printStackTrace();
            return ToolsUtil.returnMessage(0,"添加失败");
        }
    }

    /**
     * @Author: Ding XiJie
     * @Description: 修改员工
     */
    @RequestMapping("/update")
    @ResponseBody
    public Map<String, Object> update(YG yg){
        try {
            yg = ygService.update(yg);
            return ToolsUtil.returnMessage(0,"修改成功");
        }catch (Exception e){
            e.printStackTrace();
            return ToolsUtil.returnMessage(0,"修改失败");
        }
    }

    /**
     * @Author: Ding XiJie
     * @Description: 删除员工
     */
    @RequestMapping("/delete")
    @ResponseBody
    public Map<String, Object> delete(Integer yid, Model model){
        try {
            ygService.delete(yid);
            return ToolsUtil.returnMessage(0,"删除成功");
        }catch (Exception e){
            e.printStackTrace();
            return ToolsUtil.returnMessage(0,"删除失败");
        }
    }

    /**
     * @Author: Ding XiJie
     * @Description: 查询所有员工
     */
    @RequestMapping("/getAllYg")
    @ResponseBody
    public Map<String, Object> getAllYg(){
        Integer count = 0;
        try {
            List<YG> khList = ygService.getAllYg();
            count = ygService.findCount();
            return ToolsUtil.returnTable(0,count,"查询成功",khList);
        }catch (Exception e){
            e.printStackTrace();
            return ToolsUtil.returnTable(0,count,"查询失败",null);
        }
    }

    /**
     * @Author: Ding XiJie
     * @Description: 跳转员工列表页面
     */
    @RequestMapping("/toYgList")
    public String toYgList(Model model){
        return "kdgl_yg.html";
    }
}
